﻿Public Class Thief

    Dim xHealth As Integer

    Dim MaxHealth As Integer
    Dim MaxWidth As Integer
    Dim ObjectValue(1) As Integer
    Dim vSpeed As Integer

    Dim vImmovable As Boolean = False

    '0 = Value Points
    '1 = Value Money

    'Variable for Steal Skills
    Public LeftTarget As Integer
    Public TopTarget As Integer
    Public AdditionalPos(1) As Integer

    Public Property Health() As Integer
        Get
            Return xHealth
        End Get
        Set(ByVal value As Integer)
            xHealth = value
        End Set
    End Property

    Public Property Speed() As Integer
        Get
            Return vSpeed
        End Get
        Set(ByVal value As Integer)
            vSpeed = value
        End Set
    End Property

    Public Property Immovable As Boolean
        Get
            Return vImmovable
        End Get
        Set(ByVal value As Boolean)
            vImmovable = value
        End Set
    End Property

    Public Sub GoLeft()
        Me.Left -= Me.Speed
        ChangeDirection(0)
    End Sub

    Public Sub GoRight()
        Me.Left += Me.Speed
        ChangeDirection(1)
    End Sub

    Public Sub GoUp()
        Me.Top -= Me.Speed
    End Sub

    Public Sub GoDown()
        Me.Top += Me.Speed
    End Sub

    Private Sub GoToPlace(ByVal x As Integer, ByVal y As Integer)
        LeftTarget = x
        TopTarget = y

        Dim overleft As Integer
        If (Me.Left < LeftTarget) Then
            overleft = (LeftTarget - Me.Left) Mod Me.Speed
            Me.Left += overleft
            GoRight()
        ElseIf (Me.Left > LeftTarget) Then
            overleft = (Me.Left - LeftTarget) Mod Me.Speed
            Me.Left -= overleft
            GoLeft()
        End If

        Dim overtop As Integer
        If (Me.Top < TopTarget) Then
            overtop = (TopTarget - Me.Top) Mod Me.Speed
            Me.Top += overtop
            GoDown()
        ElseIf (Me.Top > TopTarget) Then
            overtop = (Me.Top - TopTarget) Mod Me.Speed
            Me.Top -= overtop
            GoUp()
        End If

    End Sub


    Public Overloads Sub DecreaseHealth(ByVal Damage As Integer)
        If (Health > 0) Then
            Health -= Damage
            If (Health <= 0) Then
                HealthBar.Width = 0
                Dead()
            Else
                HealthBar.Width = Health * MaxWidth / MaxHealth
            End If
        End If
    End Sub

    Public Overloads Sub DecreaseHealth()
        DecreaseHealth(10)
    End Sub

    Public Function GetCowAmount()
        Dim result As Integer = 0

        For Each obj As Object In Parent.Controls
            If (Immovable = False) Then
                If (TypeOf obj Is Cow) Then
                    result += 1
                End If
            Else
                If (TypeOf obj Is CowV2) Then
                    result += 1
                End If
            End If
        Next

        Return result
    End Function

    Public Sub GoToCow()
        'Search Food Function

        Dim CowAmount As Integer = GetCowAmount()

        If (CowAmount > 0) Then

            Dim ChosenCow(1) As Integer
            Dim ThiefPos(1) As Integer

            'Dim AdditionalPos(1) As Integer
            'AdditionalPos(0) = 0
            'AdditionalPos(1) = 0

            ThiefPos(0) = Me.Left - AdditionalPos(0)
            ThiefPos(1) = Me.Top - AdditionalPos(1)
            ChosenCow(0) = 0 'For Food Element
            ChosenCow(1) = Parent.Width + Parent.Height 'For Total Different

            For i As Integer = 0 To CowAmount - 1
                Dim CowPos(1) As Integer
                Dim RealThiefPos(1) As Integer

                Dim CowVar As Object

                If (Immovable = False) Then
                    CowVar = Parent.Controls.OfType(Of Cow).ElementAt(i)
                Else
                    CowVar = Parent.Controls.OfType(Of CowV2).ElementAt(i)
                End If

                Dim TotalDifferent As Integer
                Dim Different(1) As Integer

                Different(0) = 0
                Different(1) = 0

                RealThiefPos(0) = ThiefPos(0) - (CowVar.Width / 2)
                RealThiefPos(1) = ThiefPos(1) - (CowVar.Height / 2)

                CowPos(0) = CowVar.Left
                CowPos(1) = CowVar.Top

                For x As Integer = 0 To UBound(CowPos)
                    If (CowPos(x) > RealThiefPos(x)) Then
                        Different(x) = CowPos(x) - RealThiefPos(x)
                    Else
                        Different(x) = RealThiefPos(x) - CowPos(x)
                    End If
                Next

                TotalDifferent = Different.Sum

                If (TotalDifferent < ChosenCow(1)) Then
                    ChosenCow(0) = i
                    ChosenCow(1) = TotalDifferent
                End If
            Next

            Dim CowLocation(1) As Integer

            Dim CowVariable As Object

            If (Immovable = False) Then
                CowVariable = Parent.Controls.OfType(Of Cow).ElementAt(ChosenCow(0))
            Else
                CowVariable = Parent.Controls.OfType(Of CowV2).ElementAt(ChosenCow(0))
            End If

            CowLocation(0) = CowVariable.Left + AdditionalPos(0) + (CowVariable.Width / 2)
            CowLocation(1) = CowVariable.Top + AdditionalPos(1) + (CowVariable.Height / 2)

            GoToPlace(CowLocation(0), CowLocation(1))

            If (Me.Left = LeftTarget And Me.Top = TopTarget) Then
                Steal(ChosenCow(0))
            End If
        End If
    End Sub

    Public Sub Steal(ByVal CowElement As Integer)
        If (Immovable = False) Then
            Parent.Controls.OfType(Of Cow).ElementAt(CowElement).Dead(True)
        Else
            Parent.Controls.OfType(Of CowV2).ElementAt(CowElement).Dead(True)
        End If

        Me.Dispose()
    End Sub

    Private Sub Dead()
        With Parent.Controls.OfType(Of StatusPoints).ElementAtOrDefault(0)
            .AddPoints(ObjectValue(0))
            .IncreaseMoney(ObjectValue(1))
        End With

        Me.Dispose()
    End Sub

    Public Sub Pause(ByVal status As Boolean)
        Timer1.Enabled = Not status
    End Sub

    Private Sub Thief_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim acak As New Random

        PictureBox1.Image = ImageList1.Images(acak.next(2))

        MaxHealth = 100
        MaxWidth = HealthBar.Width
        ObjectValue(0) = 350
        ObjectValue(1) = 50
        Me.Speed = 2

        xHealth = MaxHealth

        Me.BringToFront()
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        DecreaseHealth()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        GoToCow()
    End Sub

    Private Sub ChangeDirection(ByVal dir As Integer)
        PictureBox1.Image = ImageList1.Images(dir)
    End Sub

End Class
